package com.kanshu.ksgb.fastread.doudou.common.view;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Color;
import android.support.annotation.IntDef;
import android.text.TextUtils;
import android.util.AttributeSet;
import android.view.View;
import android.widget.FrameLayout;
import android.widget.TextView;

import com.kanshu.ksgb.fastread.doudou.R;

import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;

/**
 *  加载、空视图
 * Created by wcy on 2017/5/10.
 */
public class EmptyLayout extends FrameLayout implements View.OnClickListener {

    public static final int STATUS_HIDE = 4;
    public static final int STATUS_LOADING = 1;
    public static final int STATUS_NO_NET = 2;
    public static final int STATUS_NO_DATA = 3;
    private Context mContext;
    private OnRetryListener mOnRetryListener;
    private int mEmptyStatus = STATUS_LOADING;
    private int mBgColor;

    TextView mTvEmptyMessage;
    View mRlEmptyContainer;
    View mEmptyLoading;
    FrameLayout mEmptyLayout;

    private String mNoDataTip;
    private int mNoDataRes;

    public EmptyLayout(Context context) {
        this(context, null);
    }

    public EmptyLayout(Context context, AttributeSet attrs) {
        super(context, attrs);
        this.mContext = context;
        init(attrs);
    }

    /**
     * 初始化
     */
    private void init(AttributeSet attrs) {
        setWillNotDraw(true);
        View.inflate(mContext, R.layout.layout_empty_loading, this);
        mTvEmptyMessage = findViewById(R.id.tv_net_error);
        mRlEmptyContainer = findViewById(R.id.rl_empty_container);
        mEmptyLoading = findViewById(R.id.empty_loading);
        mEmptyLayout = findViewById(R.id.empty_layout);
        findViewById(R.id.rl_empty_container).setOnClickListener(this);
        final TypedArray a = getContext().obtainStyledAttributes(attrs, R.styleable.EmptyLayout);
        mEmptyStatus = a.getInteger(R.styleable.EmptyLayout_status, STATUS_LOADING);
        mBgColor = a.getColor(R.styleable.EmptyLayout_bg_color, Color.WHITE);
        mEmptyLayout.setBackgroundColor(mBgColor);
        a.recycle();
        mNoDataTip = getContext().getString(R.string.empty_no_data_tip);
        mNoDataRes = R.mipmap.ic_no_data;
        _switchEmptyView();
    }

    /**
     * 隐藏视图
     */
    public void hide() {
        mEmptyStatus = STATUS_HIDE;
        _switchEmptyView();
    }

    /**
     * 设置状态
     *
     * @param emptyStatus
     */
    public void setEmptyStatus(@EmptyStatus int emptyStatus) {
        mEmptyStatus = emptyStatus;
        _switchEmptyView();
    }

    /**
     * 获取状态
     * @return  状态
     */
    public int getEmptyStatus() {
        return mEmptyStatus;
    }

    /**
     * 设置异常消息
     *
     * @param msg 显示消息
     */
    public void setEmptyMessage(String msg) {
        mTvEmptyMessage.setText(msg);
    }

    public void hideErrorIcon() {
        mTvEmptyMessage.setCompoundDrawables(null, null, null, null);
    }

    public void setErrorIcon(int resId) {
        mTvEmptyMessage.setCompoundDrawablesWithIntrinsicBounds(0, resId, 0, 0);
    }


    /**
     * 切换视图
     */
    private void _switchEmptyView() {
        switch (mEmptyStatus) {
            case STATUS_LOADING:
                setVisibility(VISIBLE);
                mRlEmptyContainer.setVisibility(GONE);
                mEmptyLoading.setVisibility(VISIBLE);
                break;
            case STATUS_NO_DATA:
                setVisibility(VISIBLE);
                mEmptyLoading.setVisibility(GONE);
                mRlEmptyContainer.setVisibility(VISIBLE);
                setEmptyMessage(mNoDataTip);
                setErrorIcon(mNoDataRes);
                break;
            case STATUS_NO_NET:
                setVisibility(VISIBLE);
                mEmptyLoading.setVisibility(GONE);
                mRlEmptyContainer.setVisibility(VISIBLE);
                setEmptyMessage(getContext().getString(R.string.empty_net_error_tip));
                setErrorIcon(R.mipmap.ic_net_error);
                break;
            case STATUS_HIDE:
                setVisibility(GONE);
                break;
        }
    }

    /**
     * 设置重试监听器
     *
     * @param retryListener 监听器
     */
    public void setRetryListener(OnRetryListener retryListener) {
        this.mOnRetryListener = retryListener;
    }


    @Override
    public void onClick(View v) {
        if (mOnRetryListener != null && mEmptyStatus != STATUS_LOADING) {
            mOnRetryListener.onRetry();
        }
    }

    /**
     * 点击重试监听器
     */
    public interface OnRetryListener {
        void onRetry();
    }

    public void setBackgroundBgColor(int color) {
        mEmptyLayout.setBackgroundColor(color);
    }

    public void setNoDataTip(String s, int res) {
        if (TextUtils.isEmpty(s)) {
            return;
        }
        mNoDataTip = s;
        mNoDataRes = res;
    }


    @Retention(RetentionPolicy.SOURCE)
    @IntDef({STATUS_LOADING, STATUS_NO_NET, STATUS_NO_DATA, STATUS_HIDE})
    public @interface EmptyStatus{}
}